import authList from "@/config/auth"

export default {
	data() {
		return {
			query: {},
			scrollPosition: 0,
		}
	},
	onLoad(query) {
		this.query = query
		this.init && this.init()
	},
	onShow() {
		const query = uni.getStorageSync("switchTabQuery")
		if (query) {
			this.query = query
			uni.removeStorageSync("switchTabQuery")
		}

		const page = getCurrentPages().pop()
		if (page?.route) {
			if (!uni.getStorageSync("token")) {
				if (!authList.includes(page.route)) {
					this.logout()
				}
			}
		}
		this.onshow && this.onshow()
	},
	onPageScroll({ scrollTop }) {
		this.scrollPosition = scrollTop
		if (scrollTop == 0) {
			this.onScrollTop && this.onScrollTop(scrollTop)
		}
		this.onScroll && this.onScroll(scrollTop)
	},
	onReachBottom() {
		this.onScrollBottom && this.onScrollBottom()
	},
	onPullDownRefresh() {
		this.onPullDown &&
			this.onPullDown(() => {
				uni.stopPullDownRefresh()
			})
	},
	/**
	 * @param {string} param0 backbutton | navigateBack
	 */
	async onBackPress({ from }) {
		if (typeof this.onBack === "function") {
			try {
				const result = await this.onBack(from)
				return !result
			} catch (error) {
				return true
			}
		}
		return false
	},
	methods: {
		startPullDown() {
			uni.startPullDownRefresh()
		},
		stopPullDown() {
			uni.stopPullDownRefresh()
		},
	},
}
